package com.starwalking.starwalkingaiagent.tools;

import cn.hutool.core.io.FileUtil;
import com.starwalking.starwalkingaiagent.constant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

/**
 * 文件操作工具类(提供文件读写的功能)
 */
public class FileOperationTool {

    private static final String FILE_DIR = FileConstant.FILE_SAVE_DIR + "/file";

    @Tool(description = "读取文件内容")
    public String readFile(@ToolParam(description = "文件名") String fileName) {
        String filePath = FILE_DIR + "/" + fileName;
        try {
            return FileUtil.readUtf8String(filePath);
        } catch (Exception e) {
            return "读取文件失败" + e.getMessage();
        }
    }

    @Tool(description = "写入文件内容")
    public String writeFile(@ToolParam(description = "文件名") String fileName,
                            @ToolParam(description = "写入的内容") String content) {
        String filePath = FILE_DIR + "/" + fileName;
        try {
            FileUtil.mkdir(FILE_DIR);
            FileUtil.writeUtf8String(content, filePath);
            return "写入文件成功";
        } catch (Exception e) {
            return "写入文件失败" + e.getMessage();
        }
    }
}
